package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiangyi.teachingsystem.dao.LoginLogMapper;
import com.xiangyi.teachingsystem.entity.po.LoginLog;
import com.xiangyi.teachingsystem.entity.po.User;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.UserService;
import com.xiangyi.teachingsystem.validation.groups.Login;
import com.xiangyi.teachingsystem.validation.groups.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    UserService userService;
    @Autowired
    LoginLogMapper loginLogMapper;

    @PostMapping("/register")
    public Result register(@Validated(Register.class) @RequestBody User user){
        try {
            User userInfo = userService.getOne(new QueryWrapper<User>().eq("phone_num", user.getPhoneNum()));
            if (userInfo!=null)
                return Result.failure("手机号已注册");
            user.setName(user.getPhoneNum());
            userService.save(user);
            return Result.success("注册成功").setData(user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/login")
    public Result login(@Validated(Login.class) @RequestBody User user){
        String phoneNum = user.getPhoneNum();
        String jobNum = user.getJobNum();
        String passWd = user.getPassWd();
        LoginLog loginLog = new LoginLog();
        loginLog.setLastIp(user.getLastIp());
        loginLog.setLastTime(user.getLastTime());
        loginLog.setCity(user.getCity());
        //System.out.println("======================"+user.getCity());
        if (phoneNum!=null){
            User userInfoPhone = userService.getOne(new QueryWrapper<User>()
                    .eq("phone_num", phoneNum)
                    .eq("admin_sign",false));
            if (userInfoPhone==null)
                return Result.failure("手机号不存在");
            if (!userInfoPhone.getPassWd().equals(passWd))
                return Result.failure("密码错误");
            loginLog.setUserId(userInfoPhone.getId());
            loginLogMapper.insert(loginLog);
            userService.update(user,new QueryWrapper<User>().eq("phone_num",phoneNum));
            return Result.success("登录成功").setData(userInfoPhone);
        }else {
            User userInfoJob = userService.getOne(new QueryWrapper<User>()
                    .eq("job_num", jobNum)
                    .eq("admin_sign",false));
            if (userInfoJob==null)
                return Result.failure("学号不存在");
            if (!userInfoJob.getPassWd().equals(passWd))
                return Result.failure("密码错误");
            loginLog.setUserId(userInfoJob.getId());
            loginLogMapper.insert(loginLog);
            userService.update(user,new QueryWrapper<User>().eq("job_num",jobNum));
            return Result.success("登录成功").setData(userInfoJob);
        }
    }
}
